//
//  HttpTestViewController.m
//  Test
//
//  Created by chenjs on 2017/8/10.
//  Copyright © 2017年 dave. All rights reserved.
//

#import "HttpTestViewController.h"
#import <AFNetworking/AFNetworking.h>
#import <HandyFrame/UIView+LayoutMethods.h>

@interface HttpTestViewController ()

@property (nonnull, nonatomic, strong) UIButton *getjsonButton;
@property (nonnull, nonatomic, strong) AFHTTPSessionManager *httpManager;

@end

@implementation HttpTestViewController

#pragma mark - life cycle

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    [self.view setBackgroundColor:[UIColor whiteColor]];
    [self.view addSubview:self.getjsonButton];
    
    self.httpManager = [AFHTTPSessionManager manager];
}

- (void)viewDidLayoutSubviews {
    [self.getjsonButton setCt_width:100.0];
    [self.getjsonButton setCt_height:40.0];
    [self.getjsonButton topInContainer:100.0 shouldResize:NO];
    [self.getjsonButton leftInContainer:20.0 shouldResize:NO];
}
#pragma mark - custom delegate

#pragma mark - event response
- (void)getJsonByAFNetworkingGetMethod {
    [self.httpManager GET:@"http://127.0.0.1:8080/data.json" parameters:@{} progress:NULL success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
        NSDictionary *dic = responseObject;
        NSLog(@"%@",dic);
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        
    }];
}
#pragma mark - private method

#pragma mark - setter getter

- (UIButton *)getjsonButton {
    if (_getjsonButton == nil) {
        _getjsonButton = [UIButton buttonWithType:UIButtonTypeCustom];
        [_getjsonButton setTitle:@"getJson" forState:UIControlStateNormal];
        [_getjsonButton addTarget:self action:@selector(getJsonByAFNetworkingGetMethod) forControlEvents:UIControlEventTouchUpInside];
        [_getjsonButton setBackgroundColor:[UIColor grayColor]];
    }
    return _getjsonButton;
}

@end
